package com.jiechuang.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiechuang.common.bean.Menu;
import com.jiechuang.common.bean.Permission;
import com.jiechuang.common.bean.Role;
import com.jiechuang.common.bean.addOrUpdate.RoleAddOrUpdate;
import com.jiechuang.common.exception.AddException;
import com.jiechuang.common.exception.DeleteException;
import com.jiechuang.common.exception.UpdateException;
import com.jiechuang.user.dto.RoleQueryInfo;
import com.jiechuang.user.mapper.PermissionMapper;
import com.jiechuang.user.mapper.RoleMapper;
import com.jiechuang.user.service.MenuService;
import com.jiechuang.user.service.PermissionService;
import com.jiechuang.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private MenuService menuService;
    @Override
    public PageInfo<Role> getRoleList(RoleQueryInfo roleQueryInfo) {
        PageHelper.startPage(roleQueryInfo.getPageNum(), roleQueryInfo.getPageSize());
        List<Role> roleList = roleMapper.getRoleList(roleQueryInfo);
        //组装权限,菜单层级
        roleList.forEach(role -> {
            List<Permission> permissionList = permissionService.getPermissionClassList(role.getPermissionList(),3);
            role.setPermissionList(permissionList);

            List<Menu> menuListByRoleCodes = menuService.getMenuListByRoleCodes(Arrays.asList(role.getRoleCode()));
            role.setMenuList(menuListByRoleCodes);
        });
        return new PageInfo<>(roleList);
    }

    @Override
    public void addRole(RoleAddOrUpdate userAddOrUpdate) {
        int i = roleMapper.addRole(userAddOrUpdate);
        if (i == 0) {
            throw new AddException("新增失败");
        }
    }

    @Override
    public Role getRole(Integer id) {
        return roleMapper.getRole(id);
    }

    @Override
    public void updateRole(RoleAddOrUpdate userAddOrUpdate) {
        int i = roleMapper.updateRole(userAddOrUpdate);
        if (i == 0) {
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public void deleteRole(Integer id) {
        int i = roleMapper.deleteRole(id);
        if (i == 0) {
            throw new DeleteException("删除失败");
        }
    }

    @Override
    public List<Permission> deletePermissionByRoleId(Integer roleId, Integer permissionId) {
        int i = roleMapper.deletePermissionByRoleId(roleId, permissionId);
        if (i == 0) {
            throw new DeleteException("删除失败");
        }
        return permissionService.getPermissionsByRoleId(roleId);
    }

    @Override
    public void givePermissionByRoleId(Integer roleId, List<Integer> newPermissionIds) {
        List<Integer> oldPermissionIds = roleMapper.getRole(roleId).getPermissionList().stream().map(a -> {
            return a.getId();
        }).collect(Collectors.toList());
        //准备删除集合和新增集合用来批量操作
        List<Integer> deleteIds=new ArrayList<>();
        List<Integer> addIds=new ArrayList<>();
        //新集合为空时，所以权限都删除
        if(CollectionUtils.isEmpty(newPermissionIds)){
            deleteIds.addAll(oldPermissionIds);
        }else {
            //循环旧集合，新集合的不存在就删除
            for (Integer oldPermissionId : oldPermissionIds) {
                if(!newPermissionIds.contains(oldPermissionId)){
                    deleteIds.add(oldPermissionId);
                }
            }
            //循环新集合，旧集合的不存在就新增
            for (Integer newPermissionId : newPermissionIds) {
                if(!oldPermissionIds.contains(newPermissionId)){
                    addIds.add(newPermissionId);
                }
            }
        }
        if(!CollectionUtils.isEmpty(deleteIds)){
            roleMapper.batchDeletePermissionByRoleId(roleId,deleteIds);
        }
        if(!CollectionUtils.isEmpty(addIds)){
            roleMapper.batchAddPermissionByRoleId(roleId,addIds);
        }
    }
}
